function addressManager() {

    var showAddressTpl = '<div class="am-g isAddress">\n' +
        '    <div class="am-u-md-8 am-u-lg-8 am-padding-0" style="border-right: 1px dashed #EEEEEE;">\n' +
        '        <div class="am-padding-bottom">\n' +
        '            <span class="am-margin-right-sm"><img src=""></span>\n' +
        '            <span id="update-address" class="am-inline-block am-btn am-btn-sm am-btn-confirm am-radius">修改</span>\n' +
        '        </div>\n' +
        '        <div class="am-margin-top-sm">\n' +
        '            <div class="am-padding-bottom-sm">\n' +
        '                <span class="am-link-muted">收货人：</span><span id="show-name">{%name%}</span>\n' +
        '            </div>\n' +
        '            <div class="am-padding-bottom-sm">\n' +
        '                <span class="am-link-muted">联系方式：</span><span id="show-phone">{%phone%}</span>\n' +
        '            </div>\n' +
        '            <div class="am-padding-bottom-sm">\n' +
        '                <span class="am-link-muted">收货地址：</span><span id="show-address">{%address%}</span>\n' +
        '            </div>\n' +
        '        </div>\n' +
        '    </div>\n' +
        '    <div class="am-u-md-4 am-u-lg-4 am-padding-0">\n' +
        '        <div class="am-text-center am-padding-top-xl am-padding-bottom-xl">\n' +
        '            <div class="am-padding-bottom-sm"><span id="address-select" class="am-text-secondary js-modal-open" style="cursor: pointer;">地址切换</span></div>\n' +
        '            <div class=""><span id="address-add" class="am-btn am-btn-sm am-btn-default am-radius address-add">新建地址</span></div>\n' +
        '        </div>\n' +
        '    </div>\n' +
        '</div>';

    var fromAddress = '<div class="am-g noAddress">\n' +
        '    <form class="am-form am-form-horizontal create-edit-address" method="post" action="{%action%}">' +
        '<input type="hidden" name="_token" value="{%token%}">' +
        '        <div class="am-u-md-8 am-u-lg-8 am-padding-0" style="border-right: 1px dashed #EEEEEE;">\n' +
        '            <div class="am-form-group">\n' +
        '                <label class="am-u-md-2 am-u-lg-2 am-padding-right-0 am-form-label">所在地址:</label>\n' +
        '                <div class="am-u-md-10 am-u-lg-10">\n' +
        '                    <div class="layui-form layui-form-pane">\n' +
        '                        <div class="layui-form-item post-area" lay-filter="post-area"></div>\n' +
        '                    </div>\n' +
        '                </div>\n' +
        '            </div>\n' +
        '            <div class="am-form-group">\n' +
        '                <label class="am-u-md-2 am-u-lg-2 am-padding-right-0 am-form-label">详细地址:</label>\n' +
        '                <div class="am-u-md-10 am-u-lg-10">\n' +
        '                    <textarea name="address" rows="5">{%address%}</textarea>\n' +
        '                </div>\n' +
        '            </div>\n' +
        '            <div class="am-form-group am-inline-block am-u-md-6">\n' +
        '                <label class="am-u-md-4 am-u-lg-4 am-padding-right-0 am-form-label">收货人:</label>\n' +
        '                <div class="am-u-md-8 am-u-lg-8 am-padding-left-sm am-padding-right-0">\n' +
        '                    <input type="text" name="name" value="{%name%}">\n' +
        '                </div>\n' +
        '            </div>\n' +
        '            <div class="am-form-group am-inline-block am-u-md-6">\n' +
        '                <label class="am-u-md-4 am-u-lg-4 am-padding-right-0 am-form-label">联系方式:</label>\n' +
        '                <div class="am-u-md-8 am-padding-right-0">\n' +
        '                    <input type="text" name="phone" value="{%phone%}">\n' +
        '                </div>\n' +
        '            </div>\n' +
        '        </div>\n' +
        '        <div class="am-u-md-4 am-u-lg-4 am-padding-0">\n' +
        '            <div class="am-text-center am-padding-top-xl am-padding-bottom-xl">\n' +
        '                <div class="am-form-group">\n' +
        '                    <div class="am-inline-block">\n' +
        '                        <label class="am-checkbox am-danger" style="padding-top: 0;">\n' +
        '                            <input type="checkbox" name="is_default" {%checked%} value="1" data-am-ucheck> 设为默认\n' +
        '                        </label>\n' +
        '                    </div>\n' +
        '                </div>\n' +
        '                <div class="am-form-group">\n' +
        '                    <div class="am-inline-block am-margin-right">\n' +
        '                        <input id="save-address" type="button" class="am-btn am-btn-danger am-radius" style="width: 105px;" value="保存地址" />\n' +
        '                    </div>\n' +
        '                    <div class="am-inline-block">\n' +
        '                        <input type="button" id="cancel-address" class="am-btn am-btn-default am-radius" value="取消" style="width: 105px;" />\n' +
        '                    </div>\n' +
        '                </div>\n' +
        '            </div>\n' +
        '        </div>\n' +
        '    </form>\n' +
        '</div>';
    initEvent();
    initShow();

    function initShow() {
        var defaultVal = $('#address-box').data('default');
        if(defaultVal!=''&&defaultVal!=undefined){
            showArea(defaultVal);
        }else{
            formArea($('#address-box').data('add-action'))
        }
    }
    function initEvent() {
        //保存
        $('#save-address').off('click').click(function(){
            var elem = $(this).parents('form.create-edit-address');
            shopAjax({
                url:elem.attr('action'),
                data:elem.serialize(),
                callback:function (response) {
                    var  address = response.data.address;
                    $('#address-box').data('default',address);
                    showArea(address);
                    $('#post_addr').val(address.id);
                }
            })
        });

        //取消
        $('#cancel-address').off('click').click(function(){
            var defaultVal = $('#address-box').data('default');
            showArea(defaultVal);
        });

        //编辑地址
        $('#update-address').off('click').click(function(){
            var defaultVal = $('#address-box').data('default');
            var action =$('#address-box').data('edit-action');
            formArea(action,defaultVal);
        });

        //新建地址
        $('#address-add').off('click').click(function(){
            var action =$('#address-box').data('add-action');
            formArea(action);
        })

        //地址切换
        var $modal = $('#address-change');
        $('#address-select').off('click').on('click', function(e) {
            $modal.modal();
            //$modal.modal('close');
        });
        //地址选择
        $('#address-change .am-form').off('click').click(function(){
            $(this).siblings().removeClass('active').find('em').remove();
            $(this).addClass('active');
            $(this).append('<em class="am-form-selected"></em>');
        })
        //地址选择-提交
        $('#submit').off('click').click(function(){
            var checkAddr = $('#address-change .am-form.active').data('item');
            showArea(checkAddr);
            $('#post_addr').val(checkAddr.id);
            $('#address-box').data('default',checkAddr);
            $modal.modal('close');
        })
        //地址选择-取消
        $('#cancel').off('click').click(function(){
            $modal.modal('close');
        })
    }


    function showArea(address) {
        var tpl = showAddressTpl.replace('{%name%}',address.consignee)
            .replace('{%phone%}',address.phone)
            .replace('{%address%}',address.address);
        $('#address-box').html(tpl);
        initEvent();
    }
    function formArea(action,postAddr) {
        var token = $('meta[name="csrf-token"]').attr('content');
        var consignee = phone = address = isChecked ='';
        if(postAddr!=''&&postAddr!=undefined){
            consignee = postAddr.consignee;
            phone = postAddr.phone;
            address = postAddr.address.split('-').pop();
            isChecked=  postAddr.is_default == 1?'checked':'';
        }
        $('#address-box').html(
            fromAddress.replace('{%action%}',action)
                .replace('{%token%}',token)
                .replace('{%address%}',address)
                .replace('{%name%}',consignee)
                .replace('{%phone%}',phone)
                .replace('{%checked%}',isChecked)
        );
        areaShow();
        initEvent();
    }

    function areaShow() {
        layui.use(['interact'], function(){
            var defaultVal = $('#address-box').data('default');
            var selected =  '';
            if(defaultVal!=''&&defaultVal!=undefined){
                selected = defaultVal.area;
            }

            if(selected !=''){
                selected = selected.split(',')
            }else{
                selected = [3,37,38];
            }
            $.get($('#address-box').data('area-action'),function (response) {
                var interact = layui.interact;
                interact.render({
                    elem : '.post-area',
                    data : response,
                    name : 'city',
                    selected:selected,
                    hint : ['请选择省份','请选择城市','请选择县/区'],
                });
            });
        });
    }
}



